package com.itheima.controller;

import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.vo.Result;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping(value = "/register", produces = "application/json;charset=utf-8")
    public Result register(String username, String password) {
        return userService.register(username, password);
    }

    @PostMapping("/login")
    //根据用户名查询User   //判断是否查询到   //判断密码是否正确}
    public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        return userService.login(username, password);
    }

    @GetMapping("/logout")
    public Result logout(){
        return userService.logout();
    }

    //通过这里解析token获取用户信息是不合适的
    /*@GetMapping("/userinfo")
    public Result userinfo(@RequestHeader("Authorization")String token){
        return userService.getUserinfo(token);
    }*/
    @GetMapping("/userinfo")
    public Result userinfo(){
        return userService.getUserinfo();
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user){

        return userService.updateUser(user);

    }


}
